package main

import (
	"fmt"
	"sync"
)

//首先没有对mymap进行加锁，导致数据重复写入发生错误
//没有设置对应的协程运行结束通知主线程的机制
var(
	mymap map[int]int = make(map[int]int, 10)
	//定义全局互斥锁
	lock sync.Mutex
)

func test(n int){
	res := 1
	for i := 1; i<=n;i++  {
		res *= i
	}
	lock.Lock()
	mymap[n] = res
	lock.Unlock()
}

func main() {
	for i := 0; i<100;i++  {
		go test(i)
	}

	lock.Lock()
	for k, v := range mymap{
		fmt.Println(k, v)
	}
	lock.Unlock()

	fmt.Println(len(mymap))
}